<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>站点统计</title>
    <script src="./js/echarts.js"></script>
    <script src="./js/axios.js"></script>
</head>

<body>
    <div id="main" style="width: 600px;height:400px;"></div>
</body>
<script>
    var myChart = echarts.init(document.getElementById('main'));
    const option = {
        title: {
            text: '站点统计',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        series: {
            name: "站点统计",
            type: 'pie',
            data: [],
            radius: '50%'
        }
    };

    function translation(key) {
        switch (key) {
            case 'commentNum':
                return "评论数量";
            case 'threadNum':
                return "帖子数量";
            case 'userNum':
                return "用户数量";
            case 'zombieNum':
                return "僵尸数量";
            case 'plantNum':
                return "植物数量";
            default:
                return key;
        }
    }
    axios.get('http://127.0.0.1:8080/site')
        .then(({
            data
        }) => {
            for (const key in data.data) {
                if (Object.hasOwnProperty.call(data.data, key)) {
                    const element = data.data[key];
                    option.series.data.push({
                        name: translation(key),
                        value: element
                    });
                }
            }
            myChart.setOption(option);
        })
        .catch(function(error) {
            console.log(error);
        });
</script>

</html>